package com.ninthgroup.controller;

import com.alibaba.excel.EasyExcel;
import com.ninthgroup.excel.ExcelDemo;
import com.ninthgroup.vo.MedicalVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @create by: 1452863884@qq.com rcg
 * @description:
 * @create time: 2022/10/23 2:37
 * @return
 */
@Controller
@RequestMapping("/excel")
public class ExcelController {


    @PostMapping("/download")
    public void download(HttpServletResponse response,  @RequestBody ExcelDemo excel) throws IOException {

        System.out.println("excel = " + excel);
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        List<ExcelDemo> all=new ArrayList<>();
        all.add(excel);
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "no-store");
        response.addHeader("Cache-Control", "max-age=0");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ExcelDemo.class).sheet("模板").doWrite(all);

    }
    @PostMapping("/download2")
    public void download2(HttpServletResponse response, @RequestBody List<MedicalVo> medicalVos) throws IOException {


        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman

        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "no-store");
        response.addHeader("Cache-Control", "max-age=0");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), MedicalVo.class).sheet("模板").doWrite(medicalVos);

    }



}
